package com.mdlib.helper.ui.fragments.set;

import android.view.View;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.TextView;

import com.ljlib.core.util.EmptyUtils;
import com.ljlib.help.R;
import com.mdlib.helper.api.DataManager;
import com.mdlib.helper.api.network.RxHelper;
import com.mdlib.helper.api.network.RxSubscribe;
import com.mdlib.helper.base.BaseTitleFragment;
import com.mdlib.helper.model.BaseResult;
import com.mdlib.helper.utils.ToastUtil;

import butterknife.Bind;
import butterknife.OnClick;

/**
 * 作者：HQY on 16/11/18 16:52
 * 邮箱：hqy_xz@126.com
 */

public class FeedBackFragment extends BaseTitleFragment {


    @Bind(R.id.rb_feedback_one)
    RadioButton mRbFeedbackOne;
    @Bind(R.id.rb_feedback_two)
    RadioButton mRbFeedbackTwo;
    @Bind(R.id.rb_feedback_three)
    RadioButton mRbFeedbackThree;
    @Bind(R.id.rb_feedback_four)
    RadioButton mRbFeedbackFour;
    @Bind(R.id.et_feedback_content)
    EditText mEtFeedbackContent;
    @Bind(R.id.tv_feedback_submit)
    TextView mTvFeedbackSubmit;

    private String cbText = "送达时间过长";

    public static FeedBackFragment newInstance() {
        return new FeedBackFragment();
    }


    @Override
    protected int getContentLayoutId() {
        return R.layout.fragment_feedback;
    }

    @Override
    protected void initView(View rootView) {
        super.initView(rootView);
        setTitle(R.string.set_feedback, R.color.white)
                .setTitleBgColor(R.color.generic_view_bg)
                .setImgLeftListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        removeFragment();
                    }
                });
    }

    @OnClick({R.id.rb_feedback_one, R.id.rb_feedback_two, R.id.rb_feedback_three, R.id.rb_feedback_four, R.id.tv_feedback_submit})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.rb_feedback_one:
                cbText = mRbFeedbackOne.getText().toString();
                mRbFeedbackOne.setChecked(true);
                mRbFeedbackTwo.setChecked(false);
                mRbFeedbackThree.setChecked(false);
                mRbFeedbackFour.setChecked(false);
                break;
            case R.id.rb_feedback_two:
                cbText = mRbFeedbackTwo.getText().toString();
                mRbFeedbackOne.setChecked(false);
                mRbFeedbackTwo.setChecked(true);
                mRbFeedbackThree.setChecked(false);
                mRbFeedbackFour.setChecked(false);
                break;
            case R.id.rb_feedback_three:
                cbText = mRbFeedbackThree.getText().toString();
                mRbFeedbackOne.setChecked(false);
                mRbFeedbackTwo.setChecked(false);
                mRbFeedbackThree.setChecked(true);
                mRbFeedbackFour.setChecked(false);
                break;
            case R.id.rb_feedback_four:
                cbText = mRbFeedbackFour.getText().toString();
                mRbFeedbackOne.setChecked(false);
                mRbFeedbackTwo.setChecked(false);
                mRbFeedbackThree.setChecked(false);
                mRbFeedbackFour.setChecked(true);
                break;
            case R.id.tv_feedback_submit:
                String content = mEtFeedbackContent.getText().toString();
                if (EmptyUtils.isEmpty(content)) {
                    ToastUtil.showToast(getActivity().getResources().getString(R.string.generic_et_not_empty));
                    return;
                }
                sendFeedback(content);
                break;
        }
    }

    private void sendFeedback(String content) {
        startLoadingDialog(true);
        addSubscribe(DataManager.getInstance().getUserApi()
                .sendFeedback(cbText, content)
                .compose(RxHelper.<BaseResult>rxSchedulerHelper())
                .subscribe(new RxSubscribe<BaseResult>() {
                    @Override
                    protected void onSucc(BaseResult baseResult) {
                        stopLoadingDialog();
                        ToastUtil.showToast(baseResult.msg);
                        removeFragment();
                    }

                    @Override
                    protected void onFail(String msg) {
                        stopLoadingDialog();
                        ToastUtil.showToast(msg);
                    }
                }));
    }
}
